/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.domain.payment;

import java.io.Serializable;
import java.util.Comparator;

/**
 * Provides a credit card number comparator.
 * 
 * @author Scott Rossillo
 *
 */
public class CreditCardNumberComparator implements Comparator, Serializable {

	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public final int compare(Object o1, Object o2) {
		
		return compare( (CreditCard) o1, (CreditCard) o2 );
	}
	
	/**
	 * Compares the given credit cards based on their card numbers.
	 * 
	 * @param card1 the first <code>CreditCard</code> to be compared
	 * @param card2 the second <code>CreditCard</code> to be compared 
	 * 
	 * @return a negative integer, zero, or a positive integer as the first argument
	 * is less than, equal to, or greater than the second based on the results
	 * of {@link String#compareTo(Object)}
	 */
	protected int compare(CreditCard card1, CreditCard card2) {
		
		return card1.getCardNumber().compareTo(card2.getCardNumber());
	}

	/**
	 * Returns true if the given object is an instance of CreditCardNumberComparator.
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		
		if(obj == null) {
			return false;
		}
		
		return obj instanceof CreditCardNumberComparator;
	}

}
